package com.yuyou.weixin.controller;

import com.yuyou.weixin.bean.ReturnResult;
import com.yuyou.weixin.service.IFnWxSrsActivityService;
import com.yuyou.weixin.vo.resp.ActivityDetail;
import com.yuyou.weixin.vo.resp.AssistanceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author ZY
 *
 */
@Api(value="YyfnWxController",description="yyfnWx操作接口")
@Validated
@RestController
@RequestMapping(value="/api/yyfnWx")
public class YyfnWxController {

//    @Autowired
    private IFnWxSrsActivityService srsActivityService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "projectCode", paramType="query", required = true),
            @ApiImplicitParam(name = "openId", value = "助力者openId", paramType="query", required = true),
            @ApiImplicitParam(name = "exclusiveCode", value = "专属码", paramType="query", required = true),
            @ApiImplicitParam(name = "headImg", value = "头像URL", paramType="query", required = true),
            @ApiImplicitParam(name = "name", value = "名字", paramType="query", required = true)
    })
    @ApiOperation(value="发送专属码助力")
    @RequestMapping(value ="/assistance",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<AssistanceResult> center(
            @NotBlank(message="openId不能为空")  String openId,
            @NotBlank (message="projectCode不能为空") String projectCode,
            @NotNull(message="exclusiveCode不能为空") Integer exclusiveCode,
            @NotBlank (message="headImg不能为空")String headImg,
            @NotBlank (message="name不能为空")String name){

        String d_headImg=null;
        String d_name =null;
        try {
            d_name = URLDecoder.decode(name, "UTF-8");
            d_headImg = URLDecoder.decode(headImg, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("解码失败：",e);;
        }

        return srsActivityService.assistance(projectCode , openId , exclusiveCode , d_headImg , d_name);
    }


    @ApiOperation(value="更新用户海报")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "projectCode", paramType="query", required = true),
            @ApiImplicitParam(name = "mediaId", value = "mediaId", paramType="query", required = true),
            @ApiImplicitParam(name = "openId", value = "openId", paramType="query", required = true)
    })
    @RequestMapping(value ="/updateMediaId",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult updateMediaId(@NotBlank (message="openId不能为空")  String openId,
                              @NotBlank (message="projectCode不能为空") String projectCode,
                              @NotBlank (message="mediaId不能为空") String mediaId){
        srsActivityService.updateMediaId(openId , projectCode , mediaId);
        return ReturnResult.success("OK");
    }

    @ApiOperation(value="查看我的助力活动详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCode", value = "projectCode", paramType="query", required = true),
            @ApiImplicitParam(name = "activityId", value = "activityId", paramType="query", required = true)
    })
    @RequestMapping(value ="/getActivityDetail",method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<ActivityDetail> getActivityDetail(@NotNull (message="activityId不能为空")  Long activityId,
                                          @NotBlank (message="projectCode不能为空") String projectCode){
        ActivityDetail ad = srsActivityService.getActivityDetail(activityId , projectCode);
        return ReturnResult.success(ad);
    }

}
